﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IBatisNet.Common.Pagination;

using MW.Service;

namespace MW.Web
{
    public partial class Result : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            timeStart = DateTime.Now;

            if (!IsPostBack)
            {
                if (string.IsNullOrEmpty(ColumnName))
                {
                    this.lvResult.DataSource = MetaService.GetInstance().SearchMetaDCList(QueryString);
                    this.ltlKeyword.Text = QueryString;
                }
                else
                {
                    this.lvResult.DataSource = MetaService.GetInstance().SearchMetaDCListColumn(ColumnName, QueryString);

                    string strTemp = "";
                    switch (ColumnName)
                    {
                        case "title":
                            strTemp = "题名";
                            break;
                        case "creator":
                            strTemp = "创建者";
                            break;
                        case "publisher":
                            strTemp = "出版者";
                            break;
                        case "subject":
                            strTemp = "主题";
                            break;
                        case "description":
                            strTemp = "描述";
                            break;
                    }

                    this.ltlKeyword.Text = strTemp + "：" + QueryString;
                }
                this.lvResult.DataBind();

                this.txtSearchFooter.Text = QueryString;
                this.txtSearchHeader.Text = QueryString;
            }

            this.ltlNote.Text= "搜索用时 " + (DateTime.Now - timeStart).TotalSeconds.ToString("f2") + " 秒";
        }

        #region Method

        #endregion

        #region Property

        private DateTime timeStart = DateTime.Now;

        private string QueryString
        {
            get
            {
                if (string.IsNullOrEmpty(Request["q"]))
                {
                    Response.Redirect("Default.aspx");
                }
                return Request["q"];
            }
        }

        private string ColumnName
        {
            get
            {
                return Request["c"];
            }
        }

        #endregion

        #region Event

        protected void btnSearchHeader_Click(object sender, EventArgs e)
        {
            if (this.txtSearchHeader.Text.Trim().Length > 0)
            {
                Response.Redirect("Result.aspx?q=" + Server.HtmlEncode(this.txtSearchHeader.Text.Trim()));
            }
        }

        protected void btnSearchFooter_Click(object sender, EventArgs e)
        {
            if (this.txtSearchFooter.Text.Trim().Length > 0)
            {
                Response.Redirect("Result.aspx?q=" + Server.HtmlEncode(this.txtSearchFooter.Text.Trim()));
            }
        }

        #endregion
    }
}
